import GameLoadingState from "./States/GameStates/GameLoadingState";
import ChapterChangeState from "./States/GameStates/ChapterChangeState";
import { GameState, EnvironmentType } from "../ADCore/Interfaces/IEnum";
import { IGameStartup } from "../ADCore/Interfaces/IGameManager";
import { IStateMachineManager } from "../ADCore/Interfaces/IStateMachine";
import ADQ from "../ADCore/Core/ADQ";
import HttpGloableConfig from "../ADCore/Network/HttpGloableConfig";

export default class GameStartup implements IGameStartup {
  onInitialize(): void {
    ADQ.IdName = "sid";
    //根据发布环境设置资源路径
    if (ADQ.GameEnvironment.environment == EnvironmentType.Prodction)
      Laya.URL.basePath = ADQ.GameEnvironment.urlResouce;
    //设置接口请求路径
    HttpGloableConfig.baseUrl = ADQ.GameEnvironment.urlAPI;
    HttpGloableConfig.headers.post["Content-Type"] = "application/json";
    this.onRegisterGameState();
  }

  stateMachine: IStateMachineManager;
  onRegisterGameState() {
    this.stateMachine = ADQ.getResolve("IStateMachineManager");
    this.stateMachine.setOwner(this);
    console.debug("状态机注册成功！");
    this.stateMachine.registerState(
      GameState.loading,
      new GameLoadingState(this)
    );
    this.stateMachine.registerState(
      GameState.run,
      new ChapterChangeState(this)
    );
  }

  onRunGame() {
    if (
      !this.stateMachine ||
      GameState.stop == this.stateMachine.getCurrentState()
    ) {
      this.onInitialize();
      this.stateMachine.changeState(GameState.loading);
    } else {
    }
  }
  onStopGame() {
    this.stateMachine.changeState(GameState.stop);
  }
  onExitGame() {
    this.stateMachine.changeState(GameState.exit);
  }
}
